using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : Entity{
    public static Player Instance;
    protected override void Awake()
    {
        base.Awake();
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
#if fase
        if (Input.GetKey(KeyCode.W))
        {
            //Debug.Log("您按下了W键");
            moveComp.Move(AniDir.Up);
        }
        else if (Input.GetKey(KeyCode.S))
        {
            //Debug.Log("您按下了S键");
            moveComp.Move(AniDir.Down);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            //Debug.Log("您按下了A键");
            moveComp.Move(AniDir.Left);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            //Debug.Log("您按下了D键");
            moveComp.Move(AniDir.Right);
        }
        else
        {
            moveComp.Move(AniDir.None);
        }
#endif
        if (Input.GetKey(KeyCode.Space))
        {
            Debug.Log("您按下了空格键");
        }

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        _isTrigger = true;
        _triggerResName = collision.gameObject.name;
        _triggerGameObject = collision.gameObject;
        Facade.Instance.SendEvent(GameEvent.Scene.ItemScopeEnter, null);
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        _isTrigger = false;
        _triggerGameObject = null;
        Facade.Instance.SendEvent(GameEvent.Scene.ItemScopeExit, null);
    }

    protected override void OnEnable()
    {
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonAClick, OnHandleButtonAClick);
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonLeftPressDown, OnHandleButtonLeftPressDown);
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonRightPressDown, OnHandleButtonRightPressDown);
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonUpPressDown, OnHandleButtonUpPressDown);
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonDownPressDown, OnHandleButtonDownPressDown);
        Facade.Instance.RegistEvent(GameEvent.Input.ButtonPressUp, OnHandleButtonPressUp);
    }

    protected override void OnDisable()
    {
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonAClick, OnHandleButtonAClick);
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonLeftPressDown, OnHandleButtonLeftPressDown);
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonRightPressDown, OnHandleButtonRightPressDown);
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonUpPressDown, OnHandleButtonUpPressDown);
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonDownPressDown, OnHandleButtonDownPressDown);
        Facade.Instance.UnRegistEvent(GameEvent.Input.ButtonPressUp, OnHandleButtonPressUp);
    }

    private bool _isTrigger;
    private GameObject _triggerGameObject;
    private string _triggerResName;
    object OnHandleButtonAClick(object sender)
    {
        if (_isTrigger)
        {
           
            var typeId = ItemDef.GetTypeId(_triggerResName);
            Debug.Log("增加道具:" + typeId + "(" + _triggerResName + ")");
            ItemMgr.Instance.IncrItemCount(typeId, 1);
            GameItemBox.Instance.MoveImageItem(typeId, _triggerGameObject);//坐标是临时代码，需要根据照相机映射
        }
        return null;
    }

    object OnHandleButtonLeftPressDown(object sender)
    {
        moveComp.Move(AniDir.Left);
        return null;
    }
    object OnHandleButtonRightPressDown(object sender)
    {
        moveComp.Move(AniDir.Right);
        return null;
    }
    object OnHandleButtonUpPressDown(object sender)
    {
        moveComp.Move(AniDir.Up);
        return null;
    }
    object OnHandleButtonDownPressDown(object sender)
    {
        moveComp.Move(AniDir.Down);
        return null;
    }
    object OnHandleButtonPressUp(object sender)
    {
        moveComp.Move(AniDir.None);
        return null;
    }
    
}
